<?php
require_once("config.inc.php");

function connect_db()
{
    // connect to the database
    $conn = mysql_connect(HOSTNAME,MYSQL_USER,MYSQL_PASS) or die(mysql_error());
    
    if($conn != FALSE)
    {
        // select the required database
        $sel_db = mysql_select_db(MYSQL_DB) or die(mysql_error());
        
        if($sel_db)
            return $conn;
    }
    
    // connection failed
    return null;
}

function check_login()
{    
    if(isset($_COOKIE['check']) == false)
        header("Location: ".LOGIN_PAGE);
        
    // check the cookie
    $check_cookie = $_COOKIE['check'] == sha1(strrev($_COOKIE['uname']));
    
    // both should be true for the user to be logged in or ask him to login
    if ($check_cookie == false )
        header("Location: ".LOGIN_PAGE);
}

function logout()
{
    setcookie("check","");
    check_login();
}
define('DEFTYPE',10);
?>
